package priv.tuyou.bo;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;

import priv.tuyou.enumer.Unit;

/**
 * 硬盘使用率
 * @author：涂有
 * @date 2017年1月4日 下午4:23:45
 */
public class HardUsage {
	
	/**
	 * 单位
	 */
	private Unit unit;

	/**
	 * 总的
	 */
	private float total;
	
	/**
	 * 已使用
	 */
	private float used;
	
	/**
	 * 可用
	 */
	private float free;
	
	public static HardUsage getInstance(){
		
		BigDecimal unit = new BigDecimal(1024 * 1024 * 1024);
		BigDecimal total = new BigDecimal(0);
		BigDecimal free = new BigDecimal(0);
		BigDecimal used = new BigDecimal(0);
		File[] files = File.listRoots();
		for(File file : files){
			
			BigDecimal t = new BigDecimal(file.getTotalSpace());
			BigDecimal f = new BigDecimal(file.getFreeSpace());
			total = total.add(t);
			free = free.add(f);
		}
		used = total.subtract(free);
		total = total.divide(unit);
		free = free.divide(unit);
		used = used.divide(unit);
		
		HardUsage usage = new HardUsage();
		usage.setTotal(total.setScale(2, RoundingMode.CEILING).floatValue());
		usage.setFree(free.setScale(2, RoundingMode.CEILING).floatValue());
		usage.setUsed(used.setScale(2, RoundingMode.CEILING).floatValue());
		usage.setUnit(Unit.G);
		return usage;
	}

	public Unit getUnit() {
		return unit;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public float getTotal() {
		return total;
	}

	public void setTotal(float total) {
		this.total = total;
	}

	public float getUsed() {
		return used;
	}

	public void setUsed(float used) {
		this.used = used;
	}

	public float getFree() {
		return free;
	}

	public void setFree(float left) {
		this.free = left;
	}

	@Override
	public String toString() {
		return "HardUsage [unit=" + unit + ", total=" + total + ", used=" + used + ", free=" + free + "]";
	}
}
